home *** CD-ROM | disk | FTP | other *** search
- Path: columba.udac.uu.se!news
- From: Enrico Savazzi <enrico.savazzi@pal.uu.se>
- Newsgroups: comp.lang.c++
- Subject: Re: Help! Allocating 3 dimensional or greater arrays in C++
- Date: Thu, 01 Feb 1996 15:53:43 +0100
- Organization: Uppsala University
- Message-ID: <3110D3F7.51B1@pal.uu.se>
- References: <310CD09F.1D71@ns.vvm.com>
- NNTP-Posting-Host: esavazzi.pal.uu.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b5 (WinNT; I)
-
- Arun Nair wrote:
- >
- > I have been trying to allocate a 3 dimensional array in C++.
- >
- > void main()
- > {
- > int *matrix;
- > int **pmatrix = &matrix;
- > int ***ppmatrix = &pmatrix;
- > int size;
- > matrix = new int[10];
- > pmatrix = new matrix[10];
- > ppmatrix = new pmatrix[10];
- > .
- > .
- > .
- > .
- >
- > What is wrong with the above code? I have searched the
- > C++ faqs and a couple of other books for this answer but so far
- > I have not been able to get an answer. If any of you can help me
- > in this regard I would be grateful.
- >
- > Thanks in advance,
- >
- > with best regards,
- >
- > Arun
- > (arun@ns.vvm.com)
-
- Let's say you want a three-dimensional array, 10 by 10 by 10 elements:
-
- int*** a;
- a = new int** [10];
- for (int i = 0; 1 < 10; i++)
- {
- a[i] = new int* [10];
- for (int j = 0; j < 10; j++)
- a[i][j] = new int [10];
- }
-
- You have first to create an array of pointers to pointers to integer arrays, then
- create arrays of pointers to integers, then allocate integer elements to each of these
- arrays. For higher dimensions, add more allocation loops. Access the array elements in
- the usual way (a[i][j][k]). Delete the array in reverse order. If you want to cut on
- execution time, use a one-dimensional array and do the pointer arithmetics explicitly
- - I usually do.
-
- Enrico Savazzi
-